/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.personal.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.modules.log.service.IIntegralYearLogService;
import org.springblade.modules.personal.entity.UserBaseEntity;
import org.springblade.modules.personal.vo.UserBaseVO;
import org.springblade.modules.personal.mapper.UserBaseMapper;
import org.springblade.modules.personal.service.IUserBaseService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 基本用户字段 服务实现类
 *
 * @author junxin
 * @since 2023-03-22
 */
@Service
@AllArgsConstructor
public class UserBaseServiceImpl extends BaseServiceImpl<UserBaseMapper, UserBaseEntity> implements IUserBaseService {

	@Override
	public IPage<UserBaseVO> selectUserBasePage(IPage<UserBaseVO> page, UserBaseVO userBase) {
		return page.setRecords(baseMapper.selectUserBasePage(page, userBase));
	}

	@Override
	public UserBaseEntity selectByUserId(Long userId) {
		return baseMapper.selectOne(Wrappers.<UserBaseEntity>query().lambda().eq(UserBaseEntity::getUserId,userId));
	}

	@Override
	public UserBaseEntity selectByIdcard(String idCard) {
		return baseMapper.selectOne(Wrappers.<UserBaseEntity>query().lambda().eq(UserBaseEntity::getUserNumber,idCard));
	}

	@Override
	public IPage<UserBaseVO> selectUserPage(IPage<UserBaseVO> page, UserBaseVO userBase) {
		return page.setRecords(baseMapper.selectUserPage(page, userBase));
	}

	@Override
	public IPage<UserBaseVO> selectExamStudentPage(IPage<UserBaseVO> page, UserBaseVO userBase) {
		return page.setRecords(baseMapper.selectExamStudentPage(page, userBase));
	}

	@Override
	public UserBaseVO getUserInfo(Long userId) {
		return baseMapper.getUserInfo(userId);
	}

	@Override
	public IPage<UserBaseVO> selectUserByDeptsAndIdcard(IPage<UserBaseVO> page, UserBaseVO userBaseVO) {
		return page.setRecords(baseMapper.selectUserByDeptsAndIdcard(page, userBaseVO));
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public boolean addPeriodAndIntegral(Long userId, Float integral, Integer period) {
		UserBaseEntity userBaseEntity = this.getOne(Wrappers.<UserBaseEntity>query().lambda().eq(UserBaseEntity::getUserId, userId));
		//添加个人积分
		userBaseEntity.setIntegral(integral+ userBaseEntity.getIntegral());
		userBaseEntity.setPeriod(period + userBaseEntity.getPeriod());
		return this.updateById(userBaseEntity);
	}

	@Override
	public List<UserBaseEntity> listByUserName(String userName) {
		LambdaQueryWrapper<UserBaseEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		if (StringUtils.isNotBlank(userName))lambdaQueryWrapper.like(UserBaseEntity::getUserName,userName);
		return this.list(lambdaQueryWrapper);
	}

	@Override
	public UserBaseEntity selectByUserName(String userName) {
		LambdaQueryWrapper<UserBaseEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
		if (StringUtils.isNotBlank(userName))lambdaQueryWrapper.eq(UserBaseEntity::getUserName,userName);
		return this.getOne(lambdaQueryWrapper);
	}
}
